﻿package classes 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.media.SoundLoaderContext;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.display.MovieClip;
	
	import classes.genericButton;
	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.Event;
	import flash.media.*;
	import flash.net.URLRequest;
	import flash.events.ProgressEvent;
    import flash.events.IOErrorEvent;

	public class showMusicSelection extends Sprite
	{
		private var sp:Sprite;
		private var instructions:TextField;
		private var instructionsFormat:TextFormat;
		private var spBackground:Sprite;
		private var muteButton:genericButton;
		private var unmuteButton:genericButton;
		
		private var music1:genericButton;
		private var music2:genericButton;
		private var music3:genericButton;
		
		private var sndChannel:SoundChannel;
		private var musicURL:String = "music/loop/"
		private var musicList:Array = new Array("1234_Rock_it.mp3", "A_Hoot.mp3", "Grease_Monkey.mp3", "Jack_1.mp3", "Look_Out_1.mp3", "Look_Out_2.mp3", "Opium.mp3", "Ouch_2.mp3", "Speed_Kills_1.mp3", "Speed_Kills_2.mp3", "Tabla_Grunge_1.mp3");
		private var reqMenu:URLRequest = new URLRequest(musicURL + musicList[0]);
		private var reqMusic1:URLRequest = new URLRequest(musicURL + musicList[4]);
		private var reqMusic2:URLRequest = new URLRequest(musicURL + musicList[8]);
		private var reqMusic3:URLRequest = new URLRequest(musicURL + musicList[10]);
		private var sndMenu:Sound;
		private var sndMusic1:Sound;
		private var sndMusic2:Sound;
		private var sndMusic3:Sound;
		private var sndTransform:SoundTransform = new SoundTransform();
		
		public function showMusicSelection():void 
		{
			sp = new Sprite();
			
			var offsetButton:uint = 3;
			
			spBackground = new Sprite();
			spBackground.graphics.beginFill(0x000000, 0.9);
			spBackground.graphics.lineStyle(1, 0x333);
			spBackground.graphics.drawRoundRect(0, 0, 157, 24, 10, 10);
			spBackground.graphics.endFill();
			sp.addChild(spBackground);
			
			instructionsFormat = new TextFormat();
			instructionsFormat.size = 9;
			instructionsFormat.font = "Verdana";
			instructions = new TextField();
			instructions.multiline = false;
			instructions.mouseEnabled = false;
			instructions.htmlText = "Music ";
			instructions.width = 40;
			instructions.height = spBackground.height;
			instructions.selectable = false;
			instructions.textColor = 0xFFFFFF;
			instructions.x = spBackground.x + offsetButton;
			instructions.y = offsetButton;
			instructions.setTextFormat(instructionsFormat);
			sp.addChild(instructions);
			
			music1 = new genericButton("1", 10, 16, 16);
			music1.name = "music1";
			music1.x = instructions.width + offsetButton * 1;
			music1.y = offsetButton;
			music1.addEventListener(MouseEvent.CLICK, onClickMusic);
			sp.addChild(music1);
			
			music2 = new genericButton("2", 10, 16, 16);
			music2.name = "music2";
			music2.x = instructions.width + music1.width + offsetButton * 2;
			music2.y = offsetButton;
			music2.addEventListener(MouseEvent.CLICK, onClickMusic);
			sp.addChild(music2);
			
			music3 = new genericButton("3", 10, 16, 16);
			music3.name = "music3";
			music3.x = instructions.width + music1.width + music2.width + offsetButton * 3;
			music3.y = offsetButton;
			music3.addEventListener(MouseEvent.CLICK, onClickMusic);
			sp.addChild(music3);
			
			muteButton = new genericButton("mute", 10, 52, 16);
			muteButton.x = instructions.width + music1.width + music2.width + music3.width + offsetButton * 4;
			muteButton.y = offsetButton;
			muteButton.addEventListener(MouseEvent.CLICK, onClickMute);
			sp.addChild(muteButton);
			
			unmuteButton = new genericButton("unMute", 10, 52, 16);
			unmuteButton.x = instructions.width + music1.width + music2.width + music3.width + offsetButton * 4;
			unmuteButton.y = offsetButton;
			unmuteButton.addEventListener(MouseEvent.CLICK, onClickUnmute);
			sp.addChild(unmuteButton);
			unmuteButton.visible = false;
			
			//sp.x = this.parent.parent.x;
			//sp.y = this.parent.parent.y;
			
			addChild(sp);
			
			sndChannel = new SoundChannel();
			sndMenu = new Sound(reqMenu);
			sndMusic1 = new Sound(reqMusic1);
			sndMusic2 = new Sound(reqMusic2);
			sndMusic3 = new Sound(reqMusic3);
			sndMenu.addEventListener(IOErrorEvent.IO_ERROR, snderrorHandler);
            sndMenu.addEventListener(ProgressEvent.PROGRESS, sndprogressHandler);
			sndMusic1.addEventListener(IOErrorEvent.IO_ERROR, snderrorHandler);
            sndMusic1.addEventListener(ProgressEvent.PROGRESS, sndprogressHandler);
			sndMusic2.addEventListener(IOErrorEvent.IO_ERROR, snderrorHandler);
            sndMusic2.addEventListener(ProgressEvent.PROGRESS, sndprogressHandler);
			sndMusic3.addEventListener(IOErrorEvent.IO_ERROR, snderrorHandler);
            sndMusic3.addEventListener(ProgressEvent.PROGRESS, sndprogressHandler);
			
			sndTransform = sndChannel.soundTransform;
            sndTransform.volume = 1;
            sndChannel.soundTransform = sndTransform;
			
			if (sndChannel != null) 
			{
				sndChannel.stop();
				sndChannel = sndMusic1.play(0,99, sndTransform);
				music1.setSelected(true);
			}
		}
		
		private function sndprogressHandler(e:ProgressEvent):void 
		{   
            //trace("loading sound..."+ 100*e.bytesLoaded/e.bytesTotal);	
        }
		
		private function snderrorHandler(e:IOErrorEvent):void 
		{
			trace("sound load error " +  e.text);
		}
		
		private function onClickMusic(e:MouseEvent):void 
		{	
			//trace("=====" + e.target.name);
			//trace("=====" + e.currentTarget.name);
			//trace("=====" + this[e.currentTarget.name].name);
			switch(e.currentTarget.name)
			{
				case "music1": 
					//trace("Music 1 selected");
					this["music1"].setSelected(true);
					this["music2"].setSelected(false);
					this["music3"].setSelected(false);
					sndChannel.stop();
					sndChannel = sndMusic1.play(0,99, sndTransform);
				break;
				case "music2": 
					//trace("Music 2 selected");
					this["music1"].setSelected(false);
					this["music2"].setSelected(true);
					this["music3"].setSelected(false);
					sndChannel.stop();
					sndChannel = sndMusic2.play(0,99, sndTransform);
				break;
				case "music3": 
					//trace("Music 3 selected");
					this["music1"].setSelected(false);
					this["music2"].setSelected(false);
					this["music3"].setSelected(true);
					sndChannel.stop();
					sndChannel = sndMusic3.play(0,99, sndTransform);
				break;
			}
		}
		
		private function onClickMute(e:MouseEvent):void 
		{
			//trace(e.target.name);
			unmuteButton.visible = true;
			muteButton.visible = false;
			sndTransform.volume = 0.05;
			sndChannel.soundTransform = sndTransform;
		}
		
		private function onClickUnmute(e:MouseEvent):void 
		{
			//trace(e.target.name);
			unmuteButton.visible = false;
			muteButton.visible = true;
			sndTransform.volume = 1;
			sndChannel.soundTransform = sndTransform;
		}
		
	}

}